<?php
/**
 * Created by PhpStorm.
 * User: hhlov_000
 * Date: 15-1-28
 * Time: 下午10:28
 */

namespace Admin\Controller;


class MessageController extends CommonController {

    public function messageList($page = 1, $rows = 10, $sort = 'id', $order = 'asc'){
        if(IS_POST){
            $message_db = M('message');
            $total = $message_db->where(array('publisher_id'=>0,'receiver_id'=>0))->count();
            $order = $sort.' '.$order;
            $limit = ($page - 1) * $rows . "," . $rows;
            //$list = $member_db->table(C('DB_PREFIX').'user M')->join(C('DB_PREFIX').'member_type MT on MT.typeid = M.typeid')->field("M.memberid,M.user_name,M.sex")->order($order)->limit($limit)->select();
            //$list = $member_db->field('user_id,user_name,sex,phone,status')->order($order)->limit($limit)->select();
            $list = $message_db->field(array('id,time,content'))->where(array('publisher_id'=>0,'receiver_id'=>0))
                ->order($order)->limit($limit)->select();
            if(!$list) $list = array();
            $data = array('total'=>$total, 'rows'=>$list);
            $this->ajaxReturn($data);
        }else{
            $menu_db = D('Menu');
            $currentpos = $menu_db->currentPos(I('get.menuid'));  //栏目位置
            $datagrid = array(
                'options'     => array(
                    'title'   => $currentpos,
                    'url'     => U('Message/messageList', array('grid'=>'datagrid')),
                    'toolbar' => 'message_messagelist_datagrid_toolbar',
                ),
                'fields' => array(
                    '发布时间'      => array('field'=>'time','width'=>15,'sortable'=>true),
                    '消息内容'    => array('field'=>'content','width'=>15,'sortable'=>true),
                    '管理操作'    => array('field'=>'id','width'=>25,'formatter'=>'messageListOperateFormatter'),
                )
            );

            $this->assign('datagrid', $datagrid);
            $this->display('message_list');
        }
    }

     public function messageAdd($info=array()){
         if(IS_POST){
             //$info = I('post.info','','trim');
             //dump($info['content']);
             $data['publisher_id'] = 0;
             $data['receiver_id'] = 0;
             $data['is_read'] = 0;
             $data['is_system'] = 0;
             $data['content'] = $info['content'];
             $data['time'] = date('Y-m-d H:i:s',time());

             $message_db = M('message');
             $message_db->add($data);
             $this->success('发布成功');

         }else{
             $this->display('message_add');
         }
     }

    public function messageDelete($id){
        $message_db = M('message');
        $result = $message_db->where(array('id'=>$id))->delete();
        if ($result){
            $this->success('删除成功');
        }else {
            $this->error('删除失败');
        }
    }

}